<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,100分)- 靠谱的车（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>程序员小明打了一辆出租车去上班。出于职业敏感&#xff0c;他注意到这辆出租车的计费表有点问题&#xff0c;总是偏大。</p> 
<p>出租车司机解释说他不喜欢数字4&#xff0c;所以改装了计费表&#xff0c;任何数字位置遇到数字4就直接跳过&#xff0c;其余功能都正常。</p> 
<p>比如&#xff1a;</p> 
<ol><li>23再多一块钱就变为25&#xff1b;</li><li>39再多一块钱变为50&#xff1b;</li><li>399再多一块钱变为500&#xff1b;</li></ol> 
<p>小明识破了司机的伎俩&#xff0c;准备利用自己的学识打败司机的阴谋。</p> 
<p>给出计费表的表面读数&#xff0c;返回实际产生的费用。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>只有一行&#xff0c;数字N&#xff0c;表示里程表的读数。</p> 
<p>(1&lt;&#61;N&lt;&#61;888888888)。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>一个数字&#xff0c;表示实际产生的费用。以回车结束。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">5</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">4</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;"> <p>5表示计费表的表面读数。</p> <p>4表示实际产生的费用其实只有4块钱。</p> </td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">17</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">15</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;"> <p>17表示计费表的表面读数。</p> <p>15表示实际产生的费用其实只有15块钱。</p> </td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">100</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">81</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;"> <p>100表示计费表的表面读数。</p> <p>81表示实际产生的费用其实只有81块钱。</p> </td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>看到题目提示输入数字N的取值范围1&lt;&#61;N&lt;&#61;888888888&#xff0c;我陷入了沉思&#xff0c;这是要我们设计一个O(1)时间复杂度的算法呀&#xff0c;因为就算是O(n)也遭不住9个8的数量级啊。</p> 
<p>原型题应该是&#xff1a;<a href="https://fcqian.blog.csdn.net/article/details/127490548" rel="nofollow" title="算法 - 里程表故障_某辆车的里程表出现了故障:它总是跳过数字3和数字8。也就是说,当前显示已走过两公-CSDN博客">算法 - 里程表故障_某辆车的里程表出现了故障:它总是跳过数字3和数字8。也就是说,当前显示已走过两公-CSDN博客</a></p> 
<p>本题的解题思路类似&#xff0c;但是不是八进制&#xff0c;而是九进制</p> 
<p><img alt="" height="767" src="https://img-blog.csdnimg.cn/b7b8f8cccfc047cea232313398e73320.png" width="837" /></p> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int[] arr &#61; Arrays.stream(sc.nextLine().split(&#34;&#34;)).mapToInt(Integer::parseInt).toArray();

    System.out.println(getResult(arr));
  }

  public static int getResult(int[] arr) {
    int correct &#61; 0;

    for (int i &#61; 0; i &lt; arr.length; i&#43;&#43;) {
      int fault &#61; arr[i];
      if (fault &gt; 4) fault--;

      for (int j &#61; arr.length - i - 1; j &gt; 0; j--) {
        fault *&#61; 9;
      }

      correct &#43;&#61; fault;
    }

    return correct;
  }
}
</code></pre> 
<p></p> 
<h4>JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const arr &#61; line.split(&#34;&#34;).map((ele) &#61;&gt; parseInt(ele));

  let correct &#61; 0;
  for (let i &#61; 0; i &lt; arr.length; i&#43;&#43;) {
    // 遍历输入数的每一位
    let fault &#61; arr[i];
    if (fault &gt; 4) {
      // 如果本位数比4大&#xff0c;则相当于跳过1次&#xff0c;则需要将本位数-1
      fault--;
    }

    for (let j &#61; arr.length - i - 1; j &gt; 0; j--) {
      // 将本位转成十进制
      fault *&#61; 9;
    }

    correct &#43;&#61; fault; // 累加各位对应十进制数
  }
  console.log(correct);
});
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
arr &#61; list(map(int, list(input())))


# 算法入口
def getResult():
    correct &#61; 0
    for i in range(len(arr)):
        fault &#61; arr[i]
        if fault &gt; 4:
            fault -&#61; 1

        for j in range(len(arr) - i - 1, 0, -1):
            fault *&#61; 9

        correct &#43;&#61; fault
    return correct


# 调用算法
print(getResult())
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;

int getResult(const int nums[], int nums_size);

int main() {
    int n;
    scanf(&#34;%d&#34;, &amp;n);

    int nums[10];
    int nums_size &#61; 0;

    while (n &gt; 0) {
        nums[nums_size&#43;&#43;] &#61; n % 10;
        n /&#61; 10;
    }

    int l &#61; 0;
    int r &#61; nums_size - 1;

    while (l &lt; r) {
        int tmp &#61; nums[l];
        nums[l] &#61; nums[r];
        nums[r] &#61; tmp;

        l&#43;&#43;;
        r--;
    }

    printf(&#34;%d\n&#34;, getResult(nums, nums_size));

    return 0;
}

int getResult(const int nums[], int nums_size) {
    int correct &#61; 0;

    for (int i &#61; 0; i &lt; nums_size; i&#43;&#43;) {
        int fault &#61; nums[i];
        if (fault &gt; 4) fault--;

        for (int j &#61; nums_size - i - 1; j &gt; 0; j--) {
            fault *&#61; 9;
        }

        correct &#43;&#61; fault;
    }

    return correct;
}


</code></pre> 
<p></p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>